package main

import (
	"net/http"
	"time"

	"gitee.com/youkelike/ziam/zshutdown"
	"gitee.com/youkelike/zlog"
	"github.com/gin-gonic/gin"
	"golang.org/x/net/context"
)

func main() {
	router := gin.Default()
	router.GET("/", func(c *gin.Context) {
		time.Sleep(time.Second * 5)
		c.String(http.StatusOK, "success.")
	})

	srv := &http.Server{
		Addr:    ":9000",
		Handler: router,
	}

	go func() {
		if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
			zlog.Fatalw("listen: %s", err)
		}
	}()

	time.AfterFunc(time.Second*10, func() {
		zshutdown.RequestShutdown()
	})

	stop := zshutdown.SetupSignalHandler()
	<-stop
	ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
	defer cancel()

	if err := srv.Shutdown(ctx); err != nil {
		zlog.Fatalw("server shutdown: %s", err)
	}
}
